// 用于全局守卫，将未携带token的接口进行拦截
import { ExecutionContext, Injectable } from "@nestjs/common";
import { AuthGuard } from "@nestjs/passport"
import { Reflector } from "@nestjs/core";
import { Observable } from "rxjs"
import { IS_PUBLIC_KEY } from "./public.decorator";
@Injectable()
export class JwtAuthGuard extends AuthGuard("jwt") {
    constructor(private reflector: Reflector) {
        super()
    }

    canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> {
        // 获取方法或类的@Public()标记
        const isPublic = this.reflector.getAllAndOverride < boolean > (IS_PUBLIC_KEY, [
            context.getHandler(),
            context.getClass()
        ])
        // 如果方法标记为@Public()，则跳过验证
        if (isPublic) return true
        // 调用父类的canActivate方法
        return super.canActivate(context)
    }
}

